package com.nm.web.advise;

import com.nm.commons.exception.ErrorsDispatcher;
import com.nm.commons.exception.SupperException;
import com.nm.web.util.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hewu on 2017/2/9 0009.
 */
@ControllerAdvice(annotations = Controller.class)
public class GlobalAdvise {

    @ExceptionHandler(value = { SupperException.class })
    public ResponseEntity<Result> handlerSuperException(Exception ex,
            HttpServletRequest request) throws Exception {

        Result r = null;

        if (ex instanceof SupperException) {
            r = new Result(((SupperException) ex).getErrCode());

            return new ResponseEntity<Result>(r, HttpStatus.OK);
        } else {
            return new ResponseEntity<Result>(
                    new Result(ErrorsDispatcher.InternalError.e()),
                    HttpStatus.OK);
        }

    }
}
